/*
 * Copyright (c) Huawei Technologies Co., Ltd. 2020-2023. All rights reserved.
 * Description: 
 * Author: g00421808
 * Create: 2/9/2023
 * Notes: 
 */

#ifndef JSBIND_ARRAY_BUFFER_H
#define JSBIND_ARRAY_BUFFER_H

#include <cstdint>

namespace Jsb {
class ArrayBuffer {
public:
    enum Typed: uint8_t {
        INT8,       // Int8Array
        UINT8,      // Uint8Array
        UINT8_C,    // Uint8ClampedArray
        INT16,      // Int16Array
        UINT16,     // Uint16Array
        INT32,      // Int32Array
        UINT32,     // Uint32Array
        FLOAT32,    // Float32Array
        FLOAT64,    // Float64Array
        BIG_INT64,  // BigInt64Array
        BIG_UINT64, // BigUint64Array
        BUFF,       // ArrayBuffer
    };

    ArrayBuffer(uint8_t* ptr = nullptr, size_t len = 0, Typed typed = BUFF) :
        data_(ptr), len_(len), typed_(typed) {}

    // 缓冲区数据地址
    inline uint8_t* data() const { return data_; }

    // 缓冲区数据大小/byte
    inline size_t length() const { return len_; }

    // 类型化
    inline Typed typed() const { return typed_; }

    inline size_t count() const
    {
        size_t count = 0;
        switch (typed_) {
            case INT8:
            case UINT8:
            case UINT8_C:
                count = len_;
                break;
            case INT16:
            case UINT16:
                count = len_ / sizeof (uint16_t);
                break;
            case INT32:
            case UINT32:
                count = len_ / sizeof (uint32_t);
                break;
            case FLOAT32:
                count = len_ / sizeof (float);
                break;
            case FLOAT64:
                count = len_ / sizeof (double);
                break;
            case BIG_INT64:
            case BIG_UINT64:
                count = len_ / sizeof (uint64_t);
                break;
            default:
                count = len_;
                break;
        }
        return count;
    }

private:
    uint8_t * data_;

    size_t len_;

    Typed typed_;
};
} // namespace Jsb

#endif //JSBIND_ARRAY_BUFFER_H
